package 链表与双指针.m03.d02.t02;

class Solution {
    public static ListNode reverse(ListNode node) {
        ListNode prev = null, cur = node;
        while(cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
    public boolean isPalindrome(ListNode head) {
        ListNode fast = head, slow = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode newHead = reverse(slow);
        while(newHead != null) {
            if (head.val == newHead.val) {
                head = head.next;
                newHead = newHead.next;
            } else {
                return false;
            }
        }
        return true;
    }
}